package com.vf.editor.mvc.scene.videoFusion;

import java.io.File;
import java.util.List;

import com.jfinal.core.Path;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;
import com.jfinal.upload.UploadFile;
import com.vf.editor.common.controller.BaseController;
import com.vf.editor.common.model.resources.SceneLayer;
import com.vf.editor.common.model.resources.SceneVmodel;
import com.vf.editor.common.util.FileUtil;
import com.vf.editor.common.util.UuidUtil;
import com.vf.editor.common.util.ZipUtil;

@Path(value = "/scene/videoFusion", viewPath = "/views/scene/videoFusion/")
public class VideoFusionController extends BaseController{

	public void library() {
		this.render("library.html");
	}
	
	public void edit() {
		SceneVmodel vmodel=SceneVmodel.dao.findById(this.getPara("id"));
		this.setAttr("vmodel", vmodel);
		this.render("edit.html");
	}
	
	public void getById() {
		this.renderSuccess("");
	}
	
	private String rootPath = PathKit.getWebRootPath();
	public void upload() {
		UploadFile uploadFile = getFile();
		try {
			String sceneId=getPara("sceneId");
			if(StrKit.isBlank(sceneId)) {
				renderError("场景ID必填!");
				return ;
			}
			String modelId=UuidUtil.getUUID();
			String path = "/upload/vmodel/"+modelId + "/" + uploadFile.getFileName().replace(" ", "");
			File file = new File(rootPath+path);
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			if (file.exists()) {
				file.delete();
			}
			
			String suffix=FileUtil.lastName(uploadFile.getFile());
			if(!StrKit.equals("zip",suffix)) {
				renderError("模型文件必须zip格式压缩!");
				return ;
			}
			if(uploadFile.getFile().renameTo(file)) {
				String filename=FileUtil.getFileNameWithoutSuffix(file);
				List<String> list=ZipUtil.unZip(file, rootPath+"/upload/vmodel/"+modelId + "/");
				if (file.exists()) {
					file.delete();
				}
				String url = null,thumbnail = null;
				if(list!=null && list.size()>0) {
					for (String string : list) {
						if(string.contains(".png") || string.contains(".jpg")) {
							thumbnail=string;
						}
						if(string.contains(".obj") ) {
							url=string;
						}
					}
					if(!StrKit.isBlank(url)) {
						SceneVmodel sceneVmodel = new SceneVmodel();
						sceneVmodel.setId(modelId);
						sceneVmodel.setSceneId(sceneId);
						sceneVmodel.setName(filename);
						sceneVmodel.setUrl("/upload/vmodel/"+modelId +"/" +url);
						sceneVmodel.setThumbnail("/upload/vmodel/"+modelId +"/" +thumbnail);
						sceneVmodel.setType("3");
						sceneVmodel.setIsRelative("1");
						if(sceneVmodel.save()) {
							SceneLayer sceneLayer=new SceneLayer();
							sceneLayer.setId(sceneVmodel.getId());
							sceneLayer.setName(sceneVmodel.getName());
							sceneLayer.setCode(sceneVmodel.getCode());
							sceneLayer.setType(sceneVmodel.getType());
							sceneLayer.setSceneId(sceneVmodel.getSceneId());
							sceneLayer.setParentId(sceneVmodel.getSceneId());
							sceneLayer.setThingId("");
							if(sceneLayer.save()) {
								sceneLayer.setThing(sceneVmodel);
								renderSuccess("上传成功！",sceneLayer);
							}else {
								renderError("上传失败！");
								sceneVmodel.delete();
								FileUtil.deleteFolder(rootPath+"/upload/vmodel/"+modelId);
							}
						}else {
							renderError("上传失败！");
							FileUtil.deleteFolder(rootPath+"/upload/vmodel/"+modelId);
						}
					}else {
						FileUtil.deleteFolder(rootPath+"/upload/vmodel/"+modelId);
						throw new Exception("格式有误，上传失败！");
					}
				}else {
					FileUtil.deleteFolder(rootPath+"/upload/vmodel/"+modelId);
					throw new Exception("格式有误，上传失败！");
				}
			}else {
				FileUtil.deleteFolder(rootPath+"/upload/vmodel/"+modelId);
				throw new Exception("上传失败！");
			}
		} catch (Exception e) {
			renderError(e.getMessage());
		}
	}
	
	public void uploadVideo() {
		UploadFile uploadFile = getFile();
		try {
			String modelId=getPara("id");
			if(StrKit.isBlank(modelId)) {
				renderError("ID必填!");
				return ;
			}
			SceneVmodel sceneVmodel = SceneVmodel.dao.findById(modelId);
			if(sceneVmodel == null) {
				throw new Exception("模型不存在，上传失败！");
			}
			
			String path = "/upload/vmodel/"+modelId + "/" + uploadFile.getFileName().replace(" ", "");
			File file = new File(rootPath+path);
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			if (file.exists()) {
				file.delete();
			}
			if(uploadFile.getFile().renameTo(file)) {
				sceneVmodel.setVideoType("0");
				sceneVmodel.setVideoUrl(path);
				sceneVmodel.setIsRelative("1");
				if(sceneVmodel.update()) {
					renderSuccess("上传成功！",sceneVmodel);
				}
			}else {
				if (file.exists()) {
					file.delete();
				}
				throw new Exception("上传失败！");
			}
		} catch (Exception e) {
			renderError(e.getMessage());
		}
	}
	
	
	public void save() {
		SceneVmodel model = getModel(SceneVmodel.class);
		if (!StrKit.isBlank(model.getId())) {
			if (model.update()) {
				SceneLayer sceneLayer=SceneLayer.dao.findById(model.getId());
				if(sceneLayer!=null) {
					sceneLayer.setId(model.getId());
					if(!StrKit.isBlank(model.getName())) sceneLayer.setName(model.getName());
					if(!StrKit.isBlank(model.getCode())) sceneLayer.setCode(model.getCode());
					if(!StrKit.isBlank(model.getType())) sceneLayer.setType(model.getType());
					sceneLayer.update();
				}
				renderSuccess("更新成功！",model);
			} else {
				renderError("更新失败！");
			}
		} else {
			if(StrKit.isBlank(model.getId())) model.setId(UuidUtil.getUUID());
			if (model.save()) {
				SceneLayer sceneLayer=new SceneLayer();
				sceneLayer.setId(model.getId());
				sceneLayer.setName(model.getName());
				sceneLayer.setCode(model.getCode());
				sceneLayer.setType(model.getType());
				sceneLayer.setSceneId(model.getSceneId());
				sceneLayer.setParentId(model.getSceneId());
				if(sceneLayer.save()) {
					renderSuccess("保存成功！",model);
				}else {
					renderError("保存失败！");
					try {
						model.delete();
					}catch (Exception e) {
					}
				}
			} else {
				renderError("保存失败！");
			}
		}
	}
	
	
	public void replace() {
		UploadFile uploadFile = getFile();
		try {
			String modelId=getPara("id");
			if(StrKit.isBlank(modelId)) {
				renderError("ID必填!");
				return ;
			}
			
			SceneVmodel sceneVmodel = SceneVmodel.dao.findById(modelId);
			if(sceneVmodel==null) {
				renderError("数据不存在，请刷新后再操作!");
				return ;
			}
			
			FileUtil.deleteFolder(rootPath+"/upload/vmodel/"+modelId);
			
			String path = "/upload/vmodel/"+modelId + "/" + uploadFile.getFileName().replace(" ", "");
			File file = new File(rootPath+path);
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			if (file.exists()) {
				file.delete();
			}
			
			String suffix=FileUtil.lastName(uploadFile.getFile());
			if(!StrKit.equals("zip",suffix)) {
				renderError("模型文件必须zip格式压缩!");
				return ;
			}
			if(uploadFile.getFile().renameTo(file)) {
				String filename=FileUtil.getFileNameWithoutSuffix(file);
				List<String> list=ZipUtil.unZip(file, rootPath+"/upload/vmodel/"+modelId + "/");
				if (file.exists()) {
					file.delete();
				}
				String url = null,thumbnail = null;
				if(list!=null && list.size()>0) {
					for (String string : list) {
						if(string.contains(".png") || string.contains(".jpg")) {
							thumbnail=string;
						}
						if(string.contains(".obj") ) {
							url=string;
						}
					}
					if(!StrKit.isBlank(url)) {
						sceneVmodel.setName(filename);
						sceneVmodel.setUrl("/upload/vmodel/"+modelId +"/" +url);
						sceneVmodel.setThumbnail("/upload/vmodel/"+modelId +"/" +thumbnail);
						if(sceneVmodel.update()) {
							SceneLayer sceneLayer= SceneLayer.dao.findById(sceneVmodel.getId());
							sceneLayer.setName(sceneVmodel.getName());
							if(sceneLayer.update()) {
								sceneLayer.setThing(sceneVmodel);
								renderSuccess("成功！",sceneVmodel);
							}else {
								renderError("失败！");
								FileUtil.deleteFolder(rootPath+"/upload/vmodel/"+modelId);
							}
						}else {
							renderError("失败！");
							FileUtil.deleteFolder(rootPath+"/upload/vmodel/"+modelId);
						}
					}else {
						FileUtil.deleteFolder(rootPath+"/upload/vmodel/"+modelId);
						throw new Exception("格式有误，上传失败！");
					}
				}else {
					FileUtil.deleteFolder(rootPath+"/upload/vmodel/"+modelId);
					throw new Exception("格式有误，上传失败！");
				}
			}else {
				FileUtil.deleteFolder(rootPath+"/upload/vmodel/"+modelId);
				throw new Exception("上传失败！");
			}
		} catch (Exception e) {
			renderError(e.getMessage());
		}
	}
	
	public void delete() {
		String id=this.getPara("id");
		if(SceneVmodel.dao.deleteById(id)) {
			SceneLayer sceneLayer= SceneLayer.dao.findById(id);
			sceneLayer.delete();
			FileUtil.deleteFolder(rootPath+"/upload/vmodel/"+id);
			this.renderSuccess("删除成功！", sceneLayer);
		}else {
			this.renderError("数据不存在！");
		}
	}
	
	public void getVmodelById() {
		SceneVmodel sceneVmodel = SceneVmodel.dao.findById(getPara("id"));
		if(sceneVmodel!=null) {
			this.renderSuccess("成功！", sceneVmodel);
		}else {
			this.renderError("数据不存在！");
		}
	}
	

}
